import difference from 'lodash/difference';
/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        // 点击排序
        if(sorter.order && sorter.field) {
            option.sortField = sorter.field;
            if(sorter.order === 'descend')
                option.isDesc = true;
            else
                option.isDesc = false;
        } else { // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};

/**
 * 计算table的 selectedRows
 * @param {*} preKeys 上一个操作 返回的 selectedKeys
 * @param {*} preRows 上一个操作 返回的 selectedRows
 * @param {*} currentKeys 本次操作 返回的 selectedKeys
 * @param {*} currentRows 本次操作 返回的 selectedRows
 * @param {*} keyIndex eg: id
 */
export const computeSelectedRows = (preKeys, preRows, currentKeys, currentRows, keyIndex = 'id') => {
    if(currentKeys.length === 0)
        return [];
    // 勾选
    if(currentKeys.length > preKeys.length) {
        const newKeys = difference(currentKeys, preKeys);
        const newRows = [];
        newKeys.forEach(key => {
            const row = currentRows.find(r => r[keyIndex] === key);
            if(row)
                newRows.push(row);
        });
        return [...preRows, ...newRows];
    } else if(currentKeys.length < preKeys.length) { // 取消勾选
        const deleteKeys = difference(preKeys, currentKeys);
        const newRows = preRows.filter(d => !deleteKeys.includes(d[keyIndex]));
        return newRows;
    }
    return preRows;
};

export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);
/* eslint-disable prefer-const, no-bitwise, no-mixed-operators */
export const uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    let r = Math.random() * 16 | 0,
        v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
});

export const getNewData = (dateTemp, days) => {
    dateTemp = dateTemp.split('-');
    let nDate = new Date(`${dateTemp[1]}-${dateTemp[2]}-${dateTemp[0]}`); //转换为MM-DD-YYYY格式
    let millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);
    let rDate = new Date(millSeconds);
    let year = rDate.getFullYear();
    let month = rDate.getMonth() + 1;
    if(month < 10) month = `0${month}`;
    let date = rDate.getDate();
    if(date < 10) date = `0${date}`;
    return (`${year}-${month}-${date}`);
};
export const getNowData = () => {
    let time = new Date();
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let date = time.getDate();
    return (`${year}-${month}-${date}`);
};
export const addMonths = n => {
    const v = new Date();
    if(v) {
        const t = new Date(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds(), v.getMilliseconds());
        t.setMonth(v.getMonth() + n);
        if(t.getDate() !== v.getDate())
            t.setDate(0);
        return t.getMonth();
    }
};
import qs from 'qs';
export const getUrl = str => {
    const r = qs.parse(str.replace('?', ''));
    if(r !== null)
        return r;
    return null;
};
